# -*- coding: UTF-8 -*-

import unittest
import os.path

from kivy.lang import Builder

from add_words import AddWords
from presenter_mock import PresenterMock
from model.gamestate import *


class AddWordsTest(unittest.TestCase):
    """add words screen test
    """

    def setUp(self):
        self.player = Player(u'Инокентий', [])
        settings = Settings(5, None)
        self.gamestate = GameState(Players([]), None, None, Phase.ADD_WORDS,
                                   None, self.player, settings)
        self.presenter = PresenterMock(Phase.START_GAME)

    def test_finish(self):
        widget = AddWords(self.gamestate, self.presenter)
        widget.finish()
        self.assertTrue(self.presenter.is_phase_changed)

    def test_on_add_word(self):
        widget = AddWords(self.gamestate, self.presenter)
        widget.word_input.text = u'кролик'
        self.assertEqual(widget.word_input.text, u'кролик')
        widget.word_list.on_add_word(widget.word_input)
        self.assertEqual(widget.word_input.text, '')
        self.assertEqual(len(self.gamestate.current_added_player.words), 1)
        self.assertEqual(self.gamestate.current_added_player.words[0].text,
                         u'кролик')
        self.assertEqual(self.gamestate.current_added_player.words[0].owner,
                         self.gamestate.current_added_player)

    def test_delete_word(self):
        widget = AddWords(self.gamestate, self.presenter)
        widget.word_input.text = u'кролик'
        widget.word_list.on_add_word(widget.word_input)
        self.assertEqual(len(self.gamestate.current_added_player.words), 1)
        widget.word_list.del_word(widget.word_list.children[0])
        self.assertEqual(len(self.gamestate.current_added_player.words), 0)

    def test_fill_from_dictionary(self):
        widget = AddWords(self.gamestate, self.presenter)
        widget.fill_from_dictionary()
        self.assertEqual(len(widget.words), 5)

    def test_set_buttons_state(self):
        widget = AddWords(self.gamestate, self.presenter)
        widget.words, widget.word_count = [None] * 10, 10
        widget.add_word_button.enabled, \
            widget.from_dictionary_button.enabled = \
            True, True
        widget.set_buttons_state()
        self.assertEquals(widget.add_word_button.enabled, False)
        self.assertEquals(widget.from_dictionary_button.enabled, False)
        widget.words, widget.word_count = [None] * 8, 10
        widget.add_word_button.enabled, \
            widget.from_dictionary_button.enabled = \
            False, False
        widget.set_buttons_state()
        self.assertEquals(widget.add_word_button.enabled, True)
        self.assertEquals(widget.from_dictionary_button.enabled, True)


if __name__ == '__main__':
    unittest.main()
